package managers.dateManager.beginFrozen.beginChange;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.apache.log4j.Logger;
import org.tbjd.client.beans.Task;
import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.constants.TimeConstants;
import org.tbjd.client.exceptions.TaskManagerException;
import org.tbjd.client.managers.TaskManager;

import factories.TaskFactory;

/**
 * <p>
 * Test TaskManager's methods
 * </p>
 * <p>
 * <b> Case</b>
 * <p>
 * A task which has multiple predecessors and those predecessors should update the task but we want the task to be updated just once
 * </p>
 * </p>
 * <p>
 * <b>Action</b>
 * </p>
 * <p>
 * BeginDate of a predecessor task is changed
 * </p>
 * <p>
 * <b> Expected</b>
 * <ul>
 * <li>update EndDate of "task"</li>
 * <li>set manual chainingMode for "task"</li>
 * <li>update successors dates</li>
 * <li>avoid multiple updates on the same successor</li>
 * </ul>
 * </p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 * 
 */
public class BeginFrozen_BeginChange_AvoidMultipleUpdatesOnSameObject_Basic extends TestCase {

	/*
	 * Task		Begins 		Ends 		Successor 	ChainingMode	task +2 days
	 * o task 	11.6.2009 	14.6.2009 	s1, s2	 	ASAP 			13.6.2009 - 16.6.2009
	 * o s1		15.6.2009 	17.6.2009 	s3			ASAP 			17.6.2009 - 19.6.2009
	 * o s2		18.6.2009 	21.6.2009 	s3			ASAP 			20.6.2009 - 23.6.2009
	 * o s3		22.6.2009 	24.6.2009 	-			ASAP 			24.6.2009 - 26.6.2009
	 */

	private Map<Integer, Object> map = null;
	private Logger log = Logger.getLogger("console");
	private DateFormat df = DateFormat.getInstance();
	private Task task = null;
	private static final long ADDED_DAYS = 2;
	private TaskManager dateManager = new TaskManager();

	protected void setUp() throws Exception {
		df = new SimpleDateFormat("dd.MM.yyyy");

		this.task = TaskFactory.getInstance();
		this.task.setName("task");
		this.task.setBeginDate(df.parse("11.6.2009"));
		this.task.setEndDate(df.parse("14.6.2009"));
		this.task.setDuration(72);

		Task s1 = TaskFactory.getInstance();
		s1.setName("s1");
		s1.setBeginDate(df.parse("15.6.2009"));
		s1.setEndDate(df.parse("17.6.2009"));
		s1.setDuration(48);

		Task s2 = TaskFactory.getInstance();
		s2.setName("s2");
		s2.setBeginDate(df.parse("18.6.2009"));
		s2.setEndDate(df.parse("21.6.2009"));
		s2.setDuration(72);

		Task s3 = TaskFactory.getInstance();
		s3.setName("s3");
		s3.setBeginDate(df.parse("22.6.2009"));
		s3.setEndDate(df.parse("24.6.2009"));
		s3.setDuration(48);

		s1.addSuccessor(s3);
		s2.addSuccessor(s3);

		this.task.addSuccessor(s1);
		this.task.addSuccessor(s2);

		super.setUp();
	}

	public void testReturnedBeanListNotNull() {
		System.out.println("");
		log.debug(" === testReturnedBeanListNotNull");
		log.debug("original BeginDate = " + df.format(this.task.getBeginDate()));
		log.debug("original EndDate = " + df.format(this.task.getEndDate()));

		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("1.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}
		log.debug("new BeginDate = " + df.format(newBeginDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call TaskManager...");
		try {
			assertNotNull("Updated bean list isn't null", dateManager.manageDates(map));

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

	}

	public void testReturnedListSize() {
		System.out.println("");
		log.debug(" === testReturnedListSizeIsOk");

		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("6.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}
		log.debug("new BeginDate = " + df.format(newBeginDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call TaskManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}
		assertEquals("Updated bean list size = 4", 4, updatedBeans.size());

	}

	public void testDates() {
		System.out.println("");
		log.debug(" === testReturnedDatesAreOk");

		Date newBeginDate = new Date(task.getBeginDate().getTime() + (ADDED_DAYS * TimeConstants.DAY_IN_MILLIS));

		log.debug("new BeginDate = " + df.format(newBeginDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call TaskManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		for (Task updatedBean : updatedBeans) {
			log.debug(updatedBean.getName() + " \t updated with BeginDate = " + df.format(updatedBean.getBeginDate()) + " , EndDate = "
					+ df.format(updatedBean.getEndDate()));
			try {
				if (updatedBean.getName().equalsIgnoreCase("task")) {
					assertEquals("check task BeginDate", df.parseObject("13.6.2009"), updatedBean.getBeginDate());
					assertEquals("check task EndDate", df.parseObject("16.6.2009"), updatedBean.getEndDate());
				}
				if (updatedBean.getName().equalsIgnoreCase("s1")) {
					assertEquals("check task BeginDate", df.parseObject("17.6.2009"), updatedBean.getBeginDate());
					assertEquals("check task EndDate", df.parseObject("19.6.2009"), updatedBean.getEndDate());
				}
				if (updatedBean.getName().equalsIgnoreCase("s2")) {
					assertEquals("check task BeginDate", df.parseObject("20.6.2009"), updatedBean.getBeginDate());
					assertEquals("check task EndDate", df.parseObject("23.6.2009"), updatedBean.getEndDate());
				}
				if (updatedBean.getName().equalsIgnoreCase("s3")) {
					assertEquals("check task BeginDate", df.parseObject("24.6.2009"), updatedBean.getBeginDate());
					assertEquals("check task EndDate", df.parseObject("26.6.2009"), updatedBean.getEndDate());
				}

			} catch (ParseException e) {
				fail(e.getMessage());

			}
		}
	}

	public void testChainingModes() {
		System.out.println("");
		log.debug(" === testChainingModes");

		Date newBeginDate = new Date(task.getBeginDate().getTime() + (ADDED_DAYS * TimeConstants.DAY_IN_MILLIS));

		log.debug("new BeginDate = " + df.format(newBeginDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call TaskManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		for (Task task : updatedBeans) {
			if (task.getName().equalsIgnoreCase("task")) {
				assertFalse("Returned bean is in manual chaining mode", task.getState().isASAPChainingMode());
			}
			if (task.getName().equalsIgnoreCase("s1")) {
				assertTrue("Returned bean is in manual chaining mode", task.getState().isASAPChainingMode());
			}
			if (task.getName().equalsIgnoreCase("s2")) {
				assertTrue("Returned bean is in manual chaining mode", task.getState().isASAPChainingMode());
			}
			if (task.getName().equalsIgnoreCase("s3")) {
				assertTrue("Returned bean is in manual chaining mode", task.getState().isASAPChainingMode());
			}
		}
	}
}